import { balance, cash, profit } from './cwbbOptions.js'
import { fillInputDone, isFillYearBeforeLast } from "../../common.js"
import { xqykjzz_Month } from '../data/xqykjzz.js'

function cwbb(data) {
  // 云南蘑菇天堂科技有限公司  91530111MA6K4LPC2J  Zbj123456  实名办税 王涛  Zbj123456  18987271395
  let isYearReport = data.saasData.isYearReport;
  let inputData = data.saasData.data.openParams.inputData;
  let sheet = $('table.table.table_head tr td b').text().replace(/\s*/g, "")
  if (isYearReport) {
    // 判断会计准则（1=>2007企业会计准则）（2=>2013小企业会计准则）
    let type = data.baseInfo.companyInfo.institution;
    // 判断是否填写上年累计
    let { companyInfo } = data.baseInfo
    let isSnlj = isFillYearBeforeLast({ companyInfo })

    // if (type.includes('2013')) {
    if (sheet.includes('现金流量表')) {
      if (!inputData.lastYearData.cash) throw new Error("时间区间错误")
      cash(inputData.data.cash, inputData.lastYearData.cash, isSnlj);
      fillInputDone({
        tax: 'cwbb', // 税种
        success: true, // 是否成功
        message: "财务报表-填报成功", // 文案
      })
    }
    if (sheet.includes('资产负债表')) {
      balance(inputData.data.balance);
      fillInputDone({
        tax: 'cwbb', // 税种
        success: true, // 是否成功
        message: "财务报表-填报成功", // 文案
      })
    }
    if (sheet.includes('利润表')) {
      if (!inputData.lastYearData.profit) throw new Error("时间区间错误")
      profit(inputData.data.profit, inputData.lastYearData.profit, isSnlj);
      fillInputDone({
        tax: 'cwbb', // 税种
        success: true, // 是否成功
        message: "财务报表-填报成功", // 文案
      })
    }
  } else {
    try {
      fillInput(data);
      fillInputDone({
        tax: 'cwbb', // 税种
        success: true, // 是否成功
        message: "财务报表-填报成功", // 文案
      })
    } catch (e) {
      fillInputDone({
        tax: 'cwbb', // 税种
        success: true, // 是否成功
        message: e.message, // 文案
      })
    }

  }
}


function fillInput(data) {
  console.log(data)
  const { saasData, baseInfo } = data;
  const { sheet, data: inputData } = saasData;
  let fillData = inputData.openParams.inputData.data[sheet];
  fillData = cwbbTransfromData({ type: sheet, isXgmnsr: baseInfo.companyInfo.taxpayerCode === '300001' }, fillData)
  for (let k in fillData) {
    let v = fillData[k];
    let input = $(`#${k}`);
    if (!input.attr('disabled')) {
      document.form1.elements[k].focus()
      input.val(`${v}`)
    }
  }
  $('body').click()
  console.log(fillData)
}

function cwbbTransfromData({ type, isXgmnsr }, data) {
  const VK = xqykjzz_Month[type];
  const obj = {};
  if (type === "balance") {
    let isEmpty = false;
    for (const key in VK) {
      const col = VK[key].replace("h", "");
      const cell = data[col];
      const xnum = key.includes("qms")
        ? cell.colValue1 || 0.0
        : cell.colValue2 || 0.0;
      obj[key] = parseFloat(xnum);
    }
    for (const key in obj) {
      let value = obj[key];
      if (value) isEmpty = true;
    }
    if (!isEmpty && !isXgmnsr) {
      obj.xx_001_qms = "0.01";
      obj.xx_015_qms = "0.01";
      obj.xx_030_qms = "0.01";
      obj.xx_051_qms = "0.01";
      obj.xx_052_qms = "0.01";
      obj.xx_053_qms = "0.01";
    }
  }
  if (type === "profit" || type === "cash") {
    for (const key in VK) {
      const col = VK[key].replace("h", "");
      const cell = data[col];
      const xnum = key.includes("sns")
        ? cell.colValue2 || 0.0
        : cell.colValue1 || 0.0;
      obj[key] = parseFloat(xnum);
    }
  }
  return obj;
}
export { cwbb }